//
//  TransformRecordVC.m
//  CBT
//
//  Created by apple on 2019/6/18.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "TransformRecordVC.h"
#import "UserManager.h"
#import "AMYestodayListCell.h"

@interface TransformRecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *contents;

@end

@implementation TransformRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = kLocalizedString(@"划转记录", nil) ;
    _contents  = [NSMutableArray array];
    _page      = 1;
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 60;
    [self.tableView registerClass:[AMYestodayListCell class] forCellReuseIdentifier:@"CELL"];
    
    [self getRecordList];
}

- (void) getRecordList {
    __weak typeof(self)weakself = self;
    [UserManager getDrawRecordListWithUserId:[CBTLocalUserManager sharedInstance].userId page:[NSString stringWithFormat:@"%d",_page] block:^(id data, HttpRequestResult result, NSString *msg) {
            if (result == kHttpRequestResultNormal) {
    
                NSDictionary *subdata = data[@"data"];
                NSArray *drawRecord = subdata[@"drawRecord"];
                if (weakself.page == 1) {
                    [weakself.contents removeAllObjects];
                }
                else {
                    if (drawRecord.count == 0) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                }
                if (nil != drawRecord) {
                    for (NSDictionary *sub in drawRecord) {
                        [weakself.contents addObject:sub];
                    }
                }
    
                [weakself.tableView reloadData];
            }
            else {
                NSLog(@"222");
            }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    AMYestodayListCell *cell = [AMYestodayListCell createTableView:tableView];
    
    NSDictionary *invests = self.contents[indexPath.row];
    
    cell.dateLabel.text     = [NSString getShowDateMonthDayWithTime:[invests[@"drawDate"] longLongValue]];
    cell.amountLabel.text   = [NSString stringWithFormat:@"-%@ PC",invests[@"amount"]];
    cell.coinNameLabel.text = @"PC";
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,15,0,15)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0,15,0,15)];
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect rect = self.view.frame;
        rect.size.height -= HFNavigationHeight;
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        
        _tableView.delegate     = self;
        _tableView.dataSource   = self;
        _tableView.scrollsToTop = YES;
        
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
        
        if (@available(iOS 11.0, *)){
            self.tableView.estimatedRowHeight = 0;
            self.tableView.estimatedSectionHeaderHeight = 0;
            self.tableView.estimatedSectionFooterHeight = 0;
        }
        
        __weak TransformRecordVC *weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page = 1;
            self.tableView.mj_footer.state = MJRefreshStateIdle;
            [weakSelf getRecordList];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.page++;
            [weakSelf getRecordList];
            [weakSelf.tableView.mj_footer endRefreshing];
        }];
        
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).triggerAutomaticallyRefreshPercent = 50;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).automaticallyRefresh = NO;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).onlyRefreshPerDrag   = YES;
    }
    
    return _tableView;
}

@end
